table = []
score = {6: 10_000, 16: 72,
         7: 36, 17: 180,
         8: 720, 18: 119,
         9: 360, 19: 36,
         10: 80, 20: 306,
         11: 252, 21: 1_080,
         12: 108, 22: 144,
         13: 72, 23: 1_800,
         14: 54, 24: 3_600,
         15: 180}

mask = [[0 for _ in range(3)]] * 3
num = [0 for _ in range(9)]
zero = (0, 0)

for i in range(3):
    line = list(map(int, input().split()))
    for index, j in enumerate(line):
        if j == 0:
            zero = (i, index)
        else:
            num[j - 1] = 1
    table.append(line)

table[zero[0]][zero[1]] = num.index(0) + 1
mask[zero[0]][zero[1]] = num.index(0) + 1

for i in range(3):
    x, y = map(int, input().split())
    mask[x - 1][y - 1] = table[x - 1][y - 1]
    print(mask[x - 1][y - 1])

x = int(input())
if 1 <= x <= 3:
    a = sum(table[x - 1])
elif 4 <= x <= 6:
    a = sum(list(zip(*table))[(x - 1) % 3])
elif x == 7:
    a = sum([table[i][i] for i in range(len(table))])
else:
    a = sum([table[i][2 - i] for i in range(len(table))])
print(score[a])
